package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.domain.StrategyCatalog;
import cn.wolfcode.trip.base.domain.StrategyContent;
import cn.wolfcode.trip.base.domain.StrategyDetail;
import cn.wolfcode.trip.base.mapper.StrategyContentMapper;
import cn.wolfcode.trip.base.mapper.StrategyDetailMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IStrategyDetailService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class StrategyDetailServiceImpl implements IStrategyDetailService {

    @Autowired
    private StrategyDetailMapper strategyDetailMapper;
    @Autowired
    private StrategyContentMapper contentMapper;


    @Override
    public int deleteByPrimaryKey(Long id) {
        return strategyDetailMapper.deleteByPrimaryKey(id);
    }


    @Override
    public StrategyDetail selectByPrimaryKey(Long id) {
        StrategyDetail strategyDetail = strategyDetailMapper.selectByPrimaryKey(id);
        StrategyContent strategyContent = contentMapper.selectByPrimaryKey(id);
        strategyDetail.setStrategyContent(strategyContent);
        return strategyDetail;
    }

    @Override
    public List<StrategyDetail> selectAll() {
        return strategyDetailMapper.selectAll();
    }

    @Override
    public void saveOrUpdate(StrategyDetail strategyDetail) {
        if (strategyDetail.getSequence() == null) {
            int max = strategyDetailMapper.selectMaxSequenceByStrategyId(strategyDetail.getStrategyCatalog().getId());
            strategyDetail.setSequence(max + 1);
        }
        //如果是发布
        if (strategyDetail.getState() == StrategyDetail.STATE_RELEASE) {
            strategyDetail.setReleaseTime(new Date());
        }

        StrategyContent strategyContent = strategyDetail.getStrategyContent();
        //设置发布时间
        if (strategyDetail.getId() == null) {
            //创建的时候设置创建时间
            strategyDetail.setCreateTime(new Date());
            strategyDetailMapper.insert(strategyDetail);
            strategyContent.setId(strategyDetail.getId());
            contentMapper.insert(strategyContent);
        } else {
            strategyDetailMapper.updateByPrimaryKey(strategyDetail);
            strategyContent.setId(strategyDetail.getId());
            contentMapper.updateByPrimaryKey(strategyContent);
        }

    }


    @Override
    public PageInfo getList(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<StrategyDetail> list = strategyDetailMapper.getList(qo);
        return new PageInfo(list);
    }

    @Override
    public Strategy getStrategyByStrategyDetailId(Long strategyDetailId) {
        Strategy strategy = strategyDetailMapper.getStrategyByStrategyDetailId(strategyDetailId);
        return strategy;
    }

    @Override
    public StrategyContent getContentByStrategyDetailId(Long detailId) {
        return contentMapper.selectByPrimaryKey(detailId);
    }

    @Override
    public List<StrategyDetail> getDetailsBycatalogId(Long id) {
        List<StrategyDetail> details = strategyDetailMapper.getDetailsBycatalogId(id);
        return details;
    }



}
